]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/InputController.cs
Stuffffffff
[rbdr/super-polarity] / Super Polarity / InputController.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework.Input;
6
7 namespace SuperPolarity
8 {
9 static class InputController
10 {
11 static Dictionary<string, List<Action<float>>> Listeners;
12 static Dictionary<string, List<Keys>> RegisteredKeys;
13 static Dictionary<string, List<Buttons>> RegisteredButtons;
14 static List<string> BlockedKeys;
15 static List<string> BlockedButtons;
16
17 static GamePadState InputGamePadState;
18 static KeyboardState InputKeyboardState;
19
20 /*
21 * Registered Events.
22 *
23 * You register: name of the event (ie. attack) and a key associated with it.
24 * or button... Left Stick /always/ dispatches move event.
25 */
26
27 static InputController()
28 {
29 Listeners = new Dictionary<string,List<Action<float>>>();
30 RegisteredButtons = new Dictionary<string, List<Buttons>>();
31 RegisteredKeys = new Dictionary<string, List<Keys>>();
32 BlockedKeys = new List<string>();
33 BlockedButtons = new List<string>();
34 InputKeyboardState = new KeyboardState();
35 InputGamePadState = new GamePadState();
36 }
37
38 public static void UpdateInput()
39 {
40 Poll();
41 DispatchMoveEvents();
42 DispatchRegisteredEvents();
43 }
44
45 private static void Poll()
46 {
47 InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
48 InputKeyboardState = Keyboard.GetState();
49 }
50
51 public static void RegisterEventForKey(string eventName, Keys key)
52 {
53 List<Keys> newKeyList;
54 if (!RegisteredKeys.ContainsKey(eventName))
55 {
56 newKeyList = new List<Keys>();
57 RegisteredKeys.Add(eventName, newKeyList);
58 }
59
60 RegisteredKeys.TryGetValue(eventName, out newKeyList);
61
62 newKeyList.Add(key);
63 }
64
65 public static void RegisterEventForButton(string eventName, Buttons button)
66 {
67 List<Buttons> newButtonList;
68 if (!RegisteredButtons.ContainsKey(eventName))
69 {
70 newButtonList = new List<Buttons>();
71 RegisteredButtons.Add(eventName, newButtonList);
72 }
73
74 RegisteredButtons.TryGetValue(eventName, out newButtonList);
75
76 newButtonList.Add(button);
77 }
78
79 private static void DispatchRegisteredEvents()
80 {
81 var keyFired = false;
82
83 foreach (KeyValuePair<string,List<Keys>> entry in RegisteredKeys) {
84 keyFired = false;
85 foreach (Keys key in entry.Value)
86 {
87 if (InputKeyboardState.IsKeyDown(key))
88 {
89 if (!BlockedKeys.Contains(entry.Key))
90 {
91 BlockedKeys.Add(entry.Key);
92 Dispatch(entry.Key, 1);
93 }
94 keyFired = true;
95 break;
96 }
97 }
98
99 if (!keyFired)
100 {
101 BlockedKeys.Remove(entry.Key);
102 }
103 }
104
105 foreach (KeyValuePair<string, List<Buttons>> entry in RegisteredButtons)
106 {
107 keyFired = false;
108 foreach (Buttons button in entry.Value)
109 {
110 if (InputGamePadState.IsButtonDown(button))
111 {
112 if (!BlockedButtons.Contains(entry.Key))
113 {
114 BlockedButtons.Add(entry.Key);
115 Dispatch(entry.Key, 1);
116 }
117 keyFired = true;
118 break;
119 };
120 }
121
122 if (!keyFired)
123 {
124 BlockedButtons.Remove(entry.Key);
125 }
126 }
127 }
128
129 private static void DispatchMoveEvents()
130 {
131 float xMovement = 0.0f;
132 float yMovement = 0.0f;
133 // Dispatch the moveX / MoveY events every frame.
134
135 xMovement = InputGamePadState.ThumbSticks.Left.X;
136 yMovement = -InputGamePadState.ThumbSticks.Left.Y;
137
138 if (InputKeyboardState.IsKeyDown(Keys.Left))
139 {
140 xMovement = -1.0f;
141 }
142
143 if (InputKeyboardState.IsKeyDown(Keys.Right))
144 {
145 xMovement = 1.0f;
146 }
147
148 if (InputKeyboardState.IsKeyDown(Keys.Up))
149 {
150 yMovement = -1.0f;
151 }
152
153 if (InputKeyboardState.IsKeyDown(Keys.Down))
154 {
155 yMovement = 1.0f;
156 }
157
158 Dispatch("moveX", xMovement);
159 Dispatch("moveY", yMovement);
160 }
161
162 public static void Bind(string eventName, Action<float> listener)
163 {
164 List<Action<float>> newListenerList;
165 List<Action<float>> listenerList;
166 bool foundListeners;
167
168 if (!Listeners.ContainsKey(eventName)) {
169 newListenerList = new List<Action<float>>();
170 Listeners.Add(eventName, newListenerList);
171 }
172
173 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
174
175 listenerList.Add(listener);
176 }
177
178 public static void Dispatch(string eventName, float value)
179 {
180 List<Action<float>> listenerList;
181 bool foundListeners;
182
183 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
184
185 if (!foundListeners)
186 {
187 return;
188 }
189
190 foreach (Action<float> method in listenerList)
191 {
192 method(value);
193 }
194 }
195
196 public static void Unlock(string eventName)
197 {
198 BlockedButtons.Remove(eventName);
199 BlockedKeys.Remove(eventName);
200 }
201 }
202 }